package cz.muni.fi.hauntedhousesweb.rest;

import com.sun.jersey.api.core.InjectParam;
import cz.muni.fi.hauntedhouses.enums.Fear;
import cz.muni.fi.hauntedhouses.service.SkillService;
import cz.muni.fi.hauntedhouses.to.SkillTO;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

@Path("/skills")
public class SkillServiceREST {
    
    @Context
    UriInfo uriInfo;
    
    @Context
    Request request;
    
    @InjectParam(value="skillService")
    SkillService skillService;
    
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public ArrayList<SkillTO> getSkills() {
        return (ArrayList<SkillTO>)(skillService.findAll());
    }
    
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("{id}")
    public SkillTO get(@PathParam("id") Long id) {
        return skillService.get(id);
    }
    
    @GET
    @Path("/name/{name}")
    @Produces(MediaType.APPLICATION_JSON)
    public SkillTO findByName(@PathParam("name") String name) {
        return skillService.findByName(name);
    }
    
    @PUT
    @Produces(MediaType.TEXT_HTML)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public void create(@FormParam("name") String name, @FormParam("power") Integer power,
                       @FormParam("causesFear") Fear causesFear, @FormParam("description") String description) throws IOException {
        SkillTO skill = new SkillTO();
        skill.setName(name);
        skill.setPower(power);
        skill.setCausesFear(causesFear);
        if (description != null) {
            skill.setDescription(description);
        }
        skillService.create(skill);
    }
    
    @POST
    @Path("{id}")
    @Produces(MediaType.TEXT_HTML)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public void update(@PathParam("id") Long id, @FormParam("name") String name, @FormParam("power") Integer power,
                       @FormParam("causesFear") Fear causesFear, @FormParam("description") String description) throws IOException {
        SkillTO skill = skillService.get(id);
        skill.setName(name);
        skill.setPower(power);
        skill.setCausesFear(causesFear);
        if (description != null) {
            skill.setDescription(description);
        }
        skillService.update(skill);
    }
    
    @DELETE
    @Path("{id}")
    public void delete(@PathParam("id") Long id) throws IOException {
        SkillTO skill = skillService.get(id);
        if (skill != null) {
            skillService.delete(skill);
        }
    }
}
